import {baseUrl, dictItemUrl} from "@/config/env";

import optionUtils from "@/option/optionUtils";
import userOption from "@/option/sys/userOption";
import userApi from "@/api/sys/userApi";

const startAdminUserOption = userOption();
const confKeyMap = {
    "def_fixed_admin_user_load": (val, formModel) => {
        return {
            type: "table",
            label: "审批人",
            display: true,
            children: startAdminUserOption,
            onLoad: (onLoadParams, callback) => {
                onLoadParams.pageDefinition = userApi.list
                onLoadParams.valueDefinition = userApi.getUserInfo
                onLoadParams.option = startAdminUserOption;
                optionUtils.tableOnLoad(onLoadParams, callback)
            },
            props: {
                label: "userFinalName",
                value: "id",
            }
        }
    },
    startUserSelfAuth: (val,formModel)=>{
        formModel.confVal = "self"
        return {
            type: "input",
            label: "审批人",
            display: false,
        }
    },
    "custom_auth_candidate": (val, formModel) => {

        return {
            type: "input",
            label: "审批人",
            display: false,
        }
    },
    "last_node_auth_user": (val, formModel) => {
        formModel.confVal = "last_node_auth_user"
        return {
            type: "input",
            label: "审批人",
            display: false,
        }
    },
    "def_fixed_role_user_load": (val, formModel) => {
        return {
            type: "tree",
            label: "角色",
            display: true,
            dicUrl: baseUrl + "/user/role/getUserRoles",
            props: {
                label: "roleName",
                value: "id",
            }
        }
    },
    "def_fixed_admin_dept_load": (val, formModel) => {
        return {
            type: "tree",
            label: "组织机构",
            display: true,
            dicUrl: baseUrl + "/start/sysDept/listTree",
            props: {
                label: "deptName",
                value: "id",
            }
        }
    },
    "def_fixed_admin_dept_manager_candidate": (val, formModel) => {
        return {
            type: "tree",
            label: "部门管理级别",
            display: true,
            dicUrl: baseUrl + "/start/sysDictionaryItem/listByDictionaryCode?dictCode=user_manage_dept_levels",
            props: {
                label: "dictName",
                value: "dictVal",
            }
        }
    },

}

function getCandidateUserOption(vm) {
    let option = {
        menuBtn: false,
        column: [
            {
                label: "审批人类型",
                type: 'select',
                prop: "confKey",
                dicUrl: dictItemUrl,
                span: 24,
                dicQuery: {
                    dictCode: "actTaskCandidateUser"
                },
                props: {
                    label: "dictName",
                    value: "dictVal",
                },
                control: (val, formModel) => {
                    let result = {}
                    if (!confKeyMap[val]) {
                        return result;
                    }
                    let configMap = confKeyMap[val];
                    let confValConfig = configMap
                    if (confValConfig instanceof Function) {
                        confValConfig = configMap(val, formModel)
                    }
                    result.confVal = confValConfig
                    vm.$nextTick(() => {
                        vm.$refs.configForm.updateDic("confVal")
                    })
                    return result
                },
            },
            {
                label: "审批人",
                type: 'select',
                prop: "confVal",
                span: 24,
                display: false,
            }
        ]
    }
    return option
}

const actTaskCandidateOption = {
    getCandidateUserOption
}
export default actTaskCandidateOption
